/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImportarFichero.java
 *
 * Created on 15-mar-2011, 12:23:06
 */

package es.fpsampayo.fin2006;

import es.fpsampayo.fin2006.importacionficheros.ImportarFicheroCat;
import es.fpsampayo.fin2006.importacionficheros.ImportarFicheroFIN2006;
import es.fpsampayo.fin2006.importacionficheros.ImportarFicheroPad;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author fran
 */
public class ImportarFichero extends javax.swing.JDialog {

    public static final int IS_FIN2006 = 0;
    public static final int IS_PADRON = 1;
    public static final int IS_CAT = 2;
    
    private int tipoFichero;
    private File file;
    private Thread hilo;
    
    ImportarFicheroPad importarpad;
    ImportarFicheroFIN2006 importarfin;
    ImportarFicheroCat importarcat;

    /** Creates new form ImportarFichero */
    public ImportarFichero(java.awt.Frame parent, boolean modal, int tipoFichero) {
        super(parent, modal);
        initComponents();
        btnCancelar.setEnabled(false);
        barProgreso.setMinimum(0);
        barProgreso.setMaximum(100);
        barProgreso.setValue(0);
        barProgreso.setStringPainted(true);
        this.tipoFichero = tipoFichero;
        
    }

    private ImportarFichero(JFrame jFrame, boolean b) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campoRutaArchivo = new javax.swing.JTextField();
        btnExaminarArchivo = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        barProgreso = new javax.swing.JProgressBar();
        txtProgreso = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        campoAnio = new javax.swing.JTextField();
        cboxConcello = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnExaminarArchivo.setText("Examinar");
        btnExaminarArchivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExaminarArchivoActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Concello");

        jLabel2.setText("Año:");

        campoAnio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoAnioActionPerformed(evt);
            }
        });

        cboxConcello.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxConcelloActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtProgreso, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(146, 146, 146))
                    .addComponent(barProgreso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboxConcello)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(campoRutaArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExaminarArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboxConcello, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoRutaArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnExaminarArchivo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtProgreso, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(barProgreso, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExaminarArchivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExaminarArchivoActionPerformed
        // TODO add your handling code here:
        
        JFileChooser filechooser = new JFileChooser();
        filechooser.setCurrentDirectory(new File("\\\\servidor\\datos\\vigo\\PADRON VIGO 2011\\PADRON 2011\\"));
        filechooser.showOpenDialog(this);
        file = filechooser.getSelectedFile();
        System.out.println(file.getAbsolutePath());
        System.setProperty("file.encoding", "UTF-8");

        if (file != null)
            campoRutaArchivo.setText(file.toString());
        else
            JOptionPane.showMessageDialog(this, "No ha seleccionado ningún archivo!");
    }//GEN-LAST:event_btnExaminarArchivoActionPerformed

    public static void setTextoProgeso (int registroActual, int totalRegistros){

        txtProgreso.setText("Registro "+ registroActual + " de " + totalRegistros);
    }
    
    public static void setBarProgresoValue (int progreso){

        barProgreso.setValue(progreso);
    }

    public void cerrar(){
        this.dispose();
    }

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        if (file == null){
            JOptionPane.showMessageDialog(this, "No ha seleccionado ningún archivo!");
        }
        else{
            
            cboxConcello.setEnabled(false);
            btnAceptar.setEnabled(false);
            btnExaminarArchivo.setEnabled(false);
            campoRutaArchivo.setEnabled(false);
            btnCancelar.setEnabled(true);
            if       (tipoFichero == IS_FIN2006){
                importarfin = new ImportarFicheroFIN2006(cboxConcello.getText(), 
                                                                        Integer.parseInt(campoAnio.getText()), 
                                                                        file);
                hilo = new Thread(importarfin);
                hilo.start();
            }else if (tipoFichero == IS_PADRON){
                importarpad = new ImportarFicheroPad(  cboxConcello.getText(), 
                                                                        Integer.parseInt(campoAnio.getText()), 
                                                                        file);
                hilo = new Thread(importarpad);
                hilo.start();
            }else if (tipoFichero == IS_CAT){
                importarcat = new ImportarFicheroCat(  cboxConcello.getText(), 
                                                                        Integer.parseInt(campoAnio.getText()), 
                                                                        file);
                hilo = new Thread(importarcat);
                hilo.start();
            }
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        if(hilo.isAlive() == true){
            if       (tipoFichero == IS_FIN2006){
                importarfin.setSalir(true);
            }else if (tipoFichero == IS_PADRON){
                importarpad.setSalir(true);
            }else if (tipoFichero == IS_CAT){
                importarcat.setSalir(true);
            }
        }else{
            JOptionPane.showMessageDialog(this, "El proceso no se está ejecutando.");
        }
        while(hilo.isAlive()){
            
        }
        JOptionPane.showMessageDialog(null, "Operación cancelada por usuario.");
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void campoAnioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoAnioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoAnioActionPerformed

    private void cboxConcelloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxConcelloActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboxConcelloActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImportarFichero dialog = new ImportarFichero(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JProgressBar barProgreso;
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExaminarArchivo;
    private javax.swing.JTextField campoAnio;
    private javax.swing.JTextField campoRutaArchivo;
    private javax.swing.JTextField cboxConcello;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private static javax.swing.JLabel txtProgreso;
    // End of variables declaration//GEN-END:variables

}
